package com.bjsc.controller.player;

import com.baomidou.mybatisplus.plugins.Page;
import com.bjsc.common.anno.Log;
import com.bjsc.service.IPlayerService;
import com.bjsc.common.bean.Rest;
import com.bjsc.common.controller.SuperController;
import com.bjsc.entity.model.InAndOut;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author create by VIC.Huang
 * @date 2018/2/25
 */
@Controller
@RequestMapping("/player")
public class PlayerController extends SuperController {
    @Autowired
    private IPlayerService playerService;

    @RequiresPermissions("listPlayer")
    @RequestMapping("/list/{pageNumber}")
    public String list(@PathVariable Integer pageNumber, String search,
                       @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        if(StringUtils.isNotBlank(search)) {
            model.addAttribute("search", search);
        }
        Page<Map<Object, Object>> page = getPage(pageNumber, pageSize);
        model.addAttribute("pageSize", pageSize);
        Page<Map<Object, Object>> pageData = playerService.selectPlayerList(page, search);
        model.addAttribute("pageData", pageData);
        return "player/list";
    }

    @RequestMapping("/betRecord/{playerId}/{pageNumber}")
    public String betRecordList(@PathVariable Long playerId,
                                @PathVariable Integer pageNumber,
                                @RequestParam(defaultValue = "10")Integer pageSize, Model model) {
        Page<Map<Object, Object>> page = getPage(pageNumber, pageSize);
        Page<Map<Object, Object>> pageData = playerService.selectBetRecords(page, playerId);
        model.addAttribute("pageData", pageData);
        model.addAttribute("playerId", playerId);
        model.addAttribute("pageSize", pageSize);
        return "player/betRecordList";
    }

    @RequestMapping("/withdrawRecord/{playerId}/{pageNumber}")
    public String withdrawRecordList(@PathVariable Long playerId,
                                @PathVariable Integer pageNumber,
                                @RequestParam(defaultValue = "10")Integer pageSize, Model model) {
        Page<Map<Object, Object>> page = getPage(pageNumber, pageSize);
        Page<Map<Object, Object>> pageData = playerService.selectWithdrawRecords(page, playerId);
        model.addAttribute("pageData", pageData);
        model.addAttribute("playerId", playerId);
        model.addAttribute("pageSize", pageSize);
        return "player/withdrawList";
    }

    @RequestMapping("/rechargeRecord/{playerId}/{pageNumber}")
    public String rechargeRecordList(@PathVariable Long playerId,
                                     @PathVariable Integer pageNumber,
                                     @RequestParam(defaultValue = "10")Integer pageSize, Model model) {
        Page<Map<Object, Object>> page = getPage(pageNumber, pageSize);
        Page<Map<Object, Object>> pageData = playerService.selectRechargeRecords(page, playerId);
        model.addAttribute("pageData", pageData);
        model.addAttribute("playerId", playerId);
        model.addAttribute("pageSize", pageSize);
        return "player/rechargeList";
    }

    @Log("冻结用户")
    @RequestMapping("/frozen")
    @ResponseBody
    public Rest frozen(@RequestParam("id")Long id) {
        if(playerService.frozenPlayer(id)) {
            return Rest.ok("冻结成功！");
        }else {
            return Rest.failure("冻结失败！");
        }
    }

    @Log("一键冻结所有用户")
    @RequestMapping("/frozenAll")
    @ResponseBody
    public Rest frozenAll() {
        if(playerService.frozenAllPlayer()) {
            return Rest.ok("冻结成功！");
        }else {
            return Rest.failure("冻结失败！");
        }
    }

    @Log("解冻用户")
    @RequestMapping("/unfrozen")
    @ResponseBody
    public Rest unfrozen(@RequestParam("id")Long id) {
        if(playerService.unfrozenPlayer(id)) {
            return Rest.ok("解冻成功！");
        }else {
            return Rest.failure("解冻失败！");
        }
    }

    @RequestMapping("/inAndOut/{playerId}")
    public String inAndOut(@PathVariable Long playerId, Model model) {
        InAndOut inAndOut = playerService.getInAndOutByPlayerId(playerId);
        model.addAttribute("totalRecharge", inAndOut.getTotalRecharge());
        model.addAttribute("totalWithdraw", inAndOut.getTotalWithdraw());
        model.addAttribute("totalCommission", inAndOut.getTotalCommission());
        model.addAttribute("totalPayback", inAndOut.getTotalPayback());
        model.addAttribute("totalWin", inAndOut.getTotalWin());
        return "player/inOut";
    }

    @RequestMapping("/invitePlayers/{playerId}/{pageNumber}")
    public String invitePlayers(@PathVariable Long playerId,
                                @PathVariable Integer pageNumber,
                                @RequestParam(defaultValue = "10")Integer pageSize, Model model) {
        Page<Map<Object, Object>> page = getPage(pageNumber, pageSize);
        Page<Map<Object, Object>> pageData = playerService.selectInvitePlayerList(page, playerId);
        model.addAttribute("pageData", pageData);
        model.addAttribute("playerId", playerId);
        model.addAttribute("pageSize", pageSize);
        return "player/invitePlayerList";
    }

    @RequestMapping("/setToSystem")
    @ResponseBody
    public Rest setToSystem(@RequestParam("id")Long id) {
        if(playerService.setToSystemPlayer(id)) {
            return Rest.ok("设置成功！");
        }else {
            return Rest.failure("设置失败！");
        }
    }

    @RequestMapping("/setToNormal")
    @ResponseBody
    public Rest setToNormal(@RequestParam("id")Long id) {
        if(playerService.setToNormalPlayer(id)) {
            return Rest.ok("设置成功！");
        }else {
            return Rest.failure("设置失败！");
        }
    }
}
